package com.testmanager.testmananger.newapiStudy.poiPractise;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CreateExcel {

    public List<String> getHeaders(){
        List<String>  list = new ArrayList<>();
        list.add("姓名");
        list.add("性别");
        list.add("年龄");
        return  list;
    }

    public List<Map<String,Object>>  getContents(){
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = null;
        for (int i =0 ;i<10;i++){
            map = new HashMap<>();
            map.put("name","张三"+i);
            map.put("sex",i%2==0?"男":"女");
            map.put("age",i);
            list.add(map);
        }
        return list;

    }



    public  void createExecl(List<String> headers, List<Map<String,Object>> contents) throws IOException {

        HSSFWorkbook workbook = new HSSFWorkbook();
       // HSSFCreationHelper creationHelper = workbook.getCreationHelper();
        HSSFSheet sheet = workbook.createSheet("人员信息");
        workbook.createSheet("人个信息");

        HSSFRow row = sheet.createRow(0);
        for (int i =0;i<headers.size();i++){
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(headers.get(i));
        }

        for (int i = 1; i<= contents.size();i++){
            HSSFRow contentRow = sheet.createRow(i);
            Map<String, Object> content = contents.get(i - 1);
            HSSFCell nameCell = contentRow.createCell(0);
            HSSFCell sexCell = contentRow.createCell(1);
            HSSFCell ageCell = contentRow.createCell(2);

            nameCell.setCellValue(content.get("name").toString());
            sexCell.setCellValue(content.get("sex").toString());
            ageCell.setCellValue(content.get("age").toString());
        }


        File file = new File("/Users/hesencun/Desktop/idea/testManagerServerSpring/src/test/java/com/testmanager/testmananger/newapiStudy/poiPractise/test1.xls");
        OutputStream os = new BufferedOutputStream(new FileOutputStream(file));


        workbook.write(os);
        os.flush();





    }



    public  <T>  HSSFWorkbook   createSheet(HSSFWorkbook workbook1, List<T> list,String name) throws IllegalAccessException {

        HSSFWorkbook workbook = workbook1;
        HSSFSheet sheet = workbook.createSheet(name);
        if (list.size()<1){
            return  workbook;
        }
        List<String> headers = getHeaders(list.get(0));
        HSSFRow headerRow = sheet.createRow(0);

        for (int i =0;i<headers.size();i++){
            HSSFCell cell = headerRow.createCell(i);
            cell.setCellValue(headers.get(i));
        }

        for (int i=0;i<list.size();i++){
            HSSFRow row = sheet.createRow(i + 1);
            setRow(row, headers, list.get(i));

        }
        return workbook;



    }

    public <T> HSSFRow setRow(HSSFRow row1 , List<String> headers,T t) throws IllegalAccessException {
        HSSFRow row = row1;
        Field[] fields = t.getClass().getDeclaredFields();
        for (Field field : fields){
            field.setAccessible(true);
            int position = -1;
            for (int i =0 ;i<headers.size();i++){
                if (field.getName().equals(headers.get(i))){
                    position = i;
                    break;
                }
            }
            HSSFCell cell = row.createCell(position);
            cell.setCellValue(field.get(t).toString());
        }

        return row;
    }




    public <T> List<String> getHeaders(T t){
        List<String> headers = new ArrayList<>();
        Field[] declaredFields = t.getClass().getDeclaredFields();
        for (Field field : declaredFields){
            field.setAccessible(true);
            headers.add(field.getName());
        }
        return headers;
    }




    public <T> void getValue(List<String> headers,  T t) throws IllegalAccessException {

        Field[] fields = t.getClass().getDeclaredFields();

        for (Field field : fields){
            field.setAccessible(true);
            int position = -1;
           for (int i =0 ;i<headers.size();i++){
               if (field.getName().equals(headers.get(i))){
                   position = i;
                   break;
               }
           }
            Object o = field.get(t);
            System.out.println(field.getName()+":::"+o);
            System.out.println(position);
        }
    }




    public static void main(String[] args) throws IOException, IllegalAccessException {
       CreateExcel createExcel = new CreateExcel();
        createExcel.createExecl(createExcel.getHeaders(),createExcel.getContents());

        List<String> headers = new ArrayList<>();
        headers.add("bb");
        headers.add("aa");
        headers.add("cc");

        Test test = new Test();
        test.setAa("2323");
        test.setBb("efef");
        test.setCc(2323);
        createExcel.getValue(headers,test);

    }

}

class Test{

    private String aa;
    private String bb;
    private int cc;

    public String getAa() {
        return aa;
    }

    public void setAa(String aa) {
        this.aa = aa;
    }

    public String getBb() {
        return bb;
    }

    public void setBb(String bb) {
        this.bb = bb;
    }

    public int getCc() {
        return cc;
    }

    public void setCc(int cc) {
        this.cc = cc;
    }
}
